package com.spstudio.iforward.end.action;

import java.io.Serializable;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.spstudio.iforward.core.bean.PageObject;
import com.spstudio.iforward.end.interfaces.ICandidateStorySessionBeanRemote;
import com.spstudio.iforward.end.interfaces.IStatisticsBeanRemote;
import com.spstudio.iforward.end.qualifier.CandidateStorySessionBeanRemoteQualifier;
import com.spstudio.iforward.end.qualifier.StatisticsStatelessBeanRemoteQualifier;
import com.spstudio.iforward.front.CandidateStory;
import com.spstudio.iforward.front.interfaces.IStorySessionBeanRemote;
import com.spstudio.iforward.front.qualifier.StorySessionBeanRemoteQualifier;

@ConversationScoped
@Named
public class CandidateStoryReviewAction implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1430931667743470807L;
	private static final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
	
	@Inject @CandidateStorySessionBeanRemoteQualifier ICandidateStorySessionBeanRemote candidateStorySessionBeanRemote;
	@Inject @StorySessionBeanRemoteQualifier IStorySessionBeanRemote storySessionBeanRemote;
	@Inject @StatisticsStatelessBeanRemoteQualifier IStatisticsBeanRemote statisticsBeanRemote;
	@Inject CandidateStory story;
	@Inject Conversation conversation;
	@Inject PageObject pageObject;
	@Inject Principal principal;
	
	private Logger log = Logger.getLogger("CandidateStoryReviewAction");
	private List<CandidateStory> candidateStoryList;
	private List<String[]> lastSevenDayPublishCount;
	private String queryTitle;
	
	@PostConstruct
	public void construct(){
		updateLastSevenDayPublishCount();
	}
	
	public void endConversation() {
		if (!conversation.isTransient()) {
			conversation.end();
		}
	}
	
	private void updateLastSevenDayPublishCount(){
		lastSevenDayPublishCount = statisticsBeanRemote.retrieveLastSevenDayPublishStoryCount();
	}
	
	public String deletePublishedStory(){
		candidateStorySessionBeanRemote.deletePublishedCandidateStory(story.getId());
		refreshCandidateStory();
		return null;
	}
	
	public String loadSingleStory(){
		CandidateStory returnStory = candidateStorySessionBeanRemote.loadSingleCandidateStory(story.getId());
		story.setTitle(returnStory.getTitle());
		story.setContent(returnStory.getContent());
		story.setComment(returnStory.getComment());
		story.setItemSource(returnStory.getItemSource());
		return null;
	}
	
	public String publishStory(){
		long publishDate = 0L;
		try{
			publishDate = format.parse(story.getPublishDateString()).getTime();
		}catch(Exception e){
			log.severe(principal.getName() + " try to publish story, but publish date is invalid with input " + publishDate);
		}
		if (publishDate != 0){
			storySessionBeanRemote.publishStory(story.getId(), publishDate);
			updateLastSevenDayPublishCount();
		}
		return refreshCandidateStory();
	}
	
	public String approveStory(){
		boolean isSuccessSetReviewedStatus = candidateStorySessionBeanRemote.setStoryStatusReviewed(story.getId());
		log.info(principal.getName() + " reviewed story (id=" + story.getId() + ") with result: " + (isSuccessSetReviewedStatus ? "OK" : "NG"));
		return refreshCandidateStory();
	}
	
	public String modifyStory(){
		return "NEW_ITEM_HOME";
	}
	
	public String deleteStory(){
		candidateStorySessionBeanRemote.deleteCandidateStory(story.getId());
		refreshCandidateStory();
		return null;
	}
	
	public String refreshCandidateStory() {
		if (conversation.isTransient()){
			conversation.begin();
		}
		candidateStoryList = candidateStorySessionBeanRemote.loadMultiCandidateStory(story.clone(), pageObject.clone()).getResultData();
		return "REVIEW_HOME";
	}
	
	public String loadCandidateStory() {
		if (conversation.isTransient()){
			conversation.begin();
		}
		CandidateStory cs = story.clone();
		cs.setTitle(queryTitle);
		candidateStoryList = candidateStorySessionBeanRemote.loadMultiCandidateStory(cs, pageObject.clone()).getResultData();
		return "REVIEW_HOME";
	}

	public String loadPrePage(){
		pageObject.tunePriorPageGroupBackend();
		candidateStoryList = candidateStorySessionBeanRemote.loadMultiCandidateStory(story.clone(), pageObject.clone()).getResultData();
		return null;
	}
	
	public String loadNextPage(){
		pageObject.tuneNextPageGroupBackend();
		candidateStoryList = candidateStorySessionBeanRemote.loadMultiCandidateStory(story.clone(), pageObject.clone()).getResultData();
		return null;
	}
	
	public String loadSpecifiedPage(){
		candidateStoryList = candidateStorySessionBeanRemote.loadMultiCandidateStory(story.clone(), pageObject.clone()).getResultData();
		return null;
	}
	
	public List<CandidateStory> getCandidateStoryList() {
		return candidateStoryList;
	}

	public void setCandidateStoryList(List<CandidateStory> candidateStoryList) {
		this.candidateStoryList = candidateStoryList;
	}

	public CandidateStory getStory() {
		return story;
	}

	public void setStory(CandidateStory story) {
		this.story = story;
	}

	public List<String[]> getLastSevenDayPublishCount() {
		return lastSevenDayPublishCount;
	}

	public void setLastSevenDayPublishCount(List<String[]> lastSevenDayPublishCount) {
		this.lastSevenDayPublishCount = lastSevenDayPublishCount;
	}

	public String getQueryTitle() {
		return queryTitle;
	}

	public void setQueryTitle(String queryTitle) {
		this.queryTitle = queryTitle;
	}
}
